package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Service("tbl_CTDT_MonHocKemService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_CTDT_MonHocKemService")
public class Tbl_CTDT_MonHocKemServiceImpl implements
		Tbl_CTDT_MonHocKemService, Serializable {

	private Log log = LogFactory.getLog(Tbl_CTDT_MonHocKemServiceImpl.class);
	private static final long serialVersionUID = -8740095022264495229L;

	@Autowired
	@Qualifier("tbl_CTDT_MonHocKemDAO")
	private Tbl_CTDT_MonHocKemDAO _tbl_CTDT_MonHocKemDao;

	@Autowired
	@Qualifier("tbl_CTDT_MonHocDAO")
	private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

	public final List<Tbl_CTDT_MonHocKem> findAlltbl_CTDT_MonHocKem(
			int pageSize, int page) {
		List<Tbl_CTDT_MonHocKem> result = get_tbl_CTDT_MonHocKemDao().findAll(
				pageSize, page);
		log.info("Number of user in DB:: " + result.size());
		return result;
	}

	@Transactional(noRollbackFor = IllegalArgumentException.class)
	public final Long createtbl_CTDT_MonHocKem(Tbl_CTDT_MonHocKem obj) {
		return _tbl_CTDT_MonHocKemDao.save(obj);
	}

	public final void updatetbl_CTDT_MonHocKem(Tbl_CTDT_MonHocKem obj) {
		_tbl_CTDT_MonHocKemDao.update(obj);
	}// end update methode

	public final Tbl_CTDT_MonHocKem gettbl_CTDT_MonHocKem(Long id) {
		log.info("Try to retrieve the tbl_CTDT_MonHocKem: " + id);
		return get_tbl_CTDT_MonHocKemDao().get(id);
	}

	public void deletetbl_CTDT_MonHocKem(Long id) {
		log.info("Try to delete the tbl_CTDT_MonHocKem by Id : " + id);
		if (_tbl_CTDT_MonHocKemDao.exists(id)) {
			_tbl_CTDT_MonHocKemDao.delete(id);
		} else {
			throw new IllegalArgumentException("EmployeeId '" + id
					+ "' does not exists");
		}
	}

	public void deleteListtbl_CTDT_MonHocKem(String[] listId) {
		for (String id : listId) {
			deletetbl_CTDT_MonHocKem(Long.parseLong(id));
		}
	}

	public List<Tbl_CTDT_MonHocKem> findtbl_CTDT_MonHocKemByCondition(
			List<ConditionReport> cons, int pageSize, int page) {
		return _tbl_CTDT_MonHocKemDao.findByCondition(Tbl_CTDT_MonHocKem.class,
				cons, pageSize, page);
	}

	public int counttbl_CTDT_MonHocKemByCondition(List<ConditionReport> cons) {
		return _tbl_CTDT_MonHocKemDao.countByCondition(
				Tbl_CTDT_MonHocKem.class, cons);
	}

	public List<Object> findColumntbl_CTDT_MonHocKemByCondition(
			List<ConditionReport> cons, List<String> columns, int pageSize,
			int page) {
		return _tbl_CTDT_MonHocKemDao.findColumnByCondition(
				Tbl_CTDT_MonHocKem.class, cons, columns, pageSize, page);
	}

	public final Tbl_CTDT_MonHocKemDAO get_tbl_CTDT_MonHocKemDao() {
		return _tbl_CTDT_MonHocKemDao;
	}

	public final void set_tbl_CTDT_MonHocKemDao(Tbl_CTDT_MonHocKemDAO obj) {
		this._tbl_CTDT_MonHocKemDao = obj;
	}

	public Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
		return _tbl_CTDT_MonHocDao;
	}

	public void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao) {
		this._tbl_CTDT_MonHocDao = _tbl_CTDT_MonHocDao;
	}

	public void createListtbl_CTDT_MonHocKemByJSON(String data) {
		try {
			ChumanoLogger.init(Tbl_CTDT_MonHocKemServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			List<Tbl_CTDT_MonHocKem> tbl_CTDT_MonHocKems = new ArrayList<Tbl_CTDT_MonHocKem>();

			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);

			for (int i = 0; i < jsons.size(); i++) {
				JSONObject obj = jsons.getJSONObject(i);
				String logStr = "";

				// F_MAMH F_MAMHKEM
				// -----------------Lay du lieu--------------------------
				String maMH = obj.getString("F_MAMH");
				String maMHKem = obj.getString("F_MAMHKEM");


				logStr = "[ " + (i + 1) + " - " 
					+ "MH: " + maMH + " - " 
					+ "MHKEM: " + maMHKem 
					+ "]";

				// ======================================================
				// tim mon hoc
				

				List<Long> monHocId = findMonHocId(maMH);
				if (monHocId == null || monHocId.size() == 0) {
					logStr += " NO MH";
					ChumanoLogger.log(logStr);
					continue;
				}
				
				List<Long> monHocKemId = findMonHocId(maMHKem);
				if (monHocKemId == null || monHocKemId.size() == 0) {
					logStr += " NO MHKEM";
					ChumanoLogger.log(logStr);
					continue;
				}
				
				
				Tbl_CTDT_MonHoc monhoc = new Tbl_CTDT_MonHoc();
				monhoc.setId(monHocId.get(0));
				Tbl_CTDT_MonHoc monhockem = new Tbl_CTDT_MonHoc();
				monhockem.setId(monHocKemId.get(0));
				//---------------------------------------
				Tbl_CTDT_MonHocKem tbl_CTDT_MonHocKem = findMonHocKem(monhoc.getId(), monhockem.getId());
				
				//-----------------------------------
				if(tbl_CTDT_MonHocKem==null) tbl_CTDT_MonHocKem = new Tbl_CTDT_MonHocKem();
				tbl_CTDT_MonHocKem.setMonHocId(monhoc);
				tbl_CTDT_MonHocKem.setMonHocKemId(monhockem);
				
				
				tbl_CTDT_MonHocKems.add(tbl_CTDT_MonHocKem);
				
				ChumanoLogger.log(logStr);

			}// end for
			
			_tbl_CTDT_MonHocKemDao.saveOrUpdateAll(tbl_CTDT_MonHocKems);
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " + e.getMessage());
			System.out.println("------------ERROR CHUMANO-------------");
			e.printStackTrace();
			System.out.println("=================================");
		}
		ChumanoLogger.stop();
	}

	private List<Long> findMonHocId(String maMonHoc) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maMonHoc);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(
				Tbl_CTDT_MonHoc.class, cons, columns, 1, 1);
		return monHocId;
	}

	private Tbl_CTDT_MonHocKem findMonHocKem(Long monhoc, Long monhockem){
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHocId.id");
			con.setValue1(monhoc);
			con.setOperator("eq");
			cons.add(con);
		}
		
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHocKemId.id");
			con.setValue1(monhockem);
			con.setOperator("eq");
			cons.add(con);
		}
		
		List<Tbl_CTDT_MonHocKem> mhkem = _tbl_CTDT_MonHocKemDao.findByCondition(Tbl_CTDT_MonHocKem.class, cons, 1, 1);
		if(mhkem==null|| mhkem.size()==0) return null;
		return mhkem.get(0);
	}
}// endclass

